<?php

namespace App\Http\Controllers;

use App\Models\Role;
use App\Models\Permission;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Http\Requests\Permission\CreateForm;
use App\Http\Requests\Permission\UpdateForm;

class PermissionController extends Controller
{
    private $permission;

    public function __construct(Permission $permission)
    {
        $this->permission = $permission;
    }

    public function index(){
        $this->validateUserPermissionRedirect('show-permissions');
        $permissions = $this->permission->getPermissionsGroup();
        return view('permission.index',compact('permissions'));
    }

    public function create(){
        $this->validateUserPermissionRedirect('create-permission');
        $permissionsList = $this->permission->getPermissionsGroup();
        return view('permission.create',compact('permissionsList'));
    }

    public function store(CreateForm $request){

        $this->validateUserPermissionRedirect('create-permission');

        DB::beginTransaction();
        try{
            $role = Role::where('name','SuperManager')->first();
            if(!$role){
                $role = Role::create(['name'=>'SuperManager','cn_name'=>'超级管理员']);
            }

            $level = 1;
            if($request->pid){
                $parentPermission = $this->permission->find($request->pid);
                $level = $parentPermission->level + 1;
            }

            if($level > 3){
                session()->flash('danger','仅允许3级权限失败');
                return  redirect()->back();
            }

            $permission = $this->permission;
            $permission->name = $request->name;
            $permission->cn_name = $request->cn_name;
            $permission->pid = $request->pid;
            $permission->level = $level;
            $permission->save();
            $role->givePermissionTo($permission->name);
            DB::commit();
            Cache::forget('all-permissions');
            session()->flash('success','创建'.$request->name.'权限成功');
            return  redirect()->route('permission.index');
        }catch (\Exception $e){
            DB::rollBack();
            session()->flash('danger','创建'.$request->name.'权限失败');
            return  redirect()->back();
        }
    }

    public function edit($id){
        $this->validateUserPermissionRedirect('edit-permission');
        $permissionsList = $this->permission->getPermissionList();
        $permission = $this->permission->find($id);
        return view('permission.edit',compact('permission','permissionsList'));
    }

    public function update(UpdateForm $request,$id){

        $this->validateUserPermissionRedirect('edit-permission');

        $permission = $this->permission->find($id);

        $level = 1;
        if($request->pid){
            $parentPermission = $this->permission->find($request->pid);
            $level = $parentPermission->level + 1;
        }

        if($level > 3){
            session()->flash('danger','仅允许3级权限失败');
            return  redirect()->back();
        }

        $permission->name = $request->name;
        $permission->cn_name = $request->cn_name;
        $permission->pid = $request->pid;
        $permission->level = $level;
        if($permission->save()){
            Cache::forget('all-permissions');
            session()->flash('success','编辑'.$request->name.'权限成功');
            return  redirect()->route('permission.index');
        }
        session()->flash('danger','编辑'.$request->name.'权限失败');
        return  redirect()->back();
    }

    public function getChildPermissions($pid = 0){
        $this->validateUserPermissionRedirect('create-role');
        $data = $this->permission->getChildPermissions($pid);
        return response()->json($data,200);
    }

    public function destroy($id){
        $this->validateUserPermissionRedirect('delete-permission');
        $permission = $this->permission->find($id);
        if($permission->delete()){
            session()->flash('success','删除'.$permission->cn_name.'权限成功');
            Cache::forget('all-permissions');
            return  redirect()->route('permission.index');
        }
    }

    public function tt(){
        $data = $this->permission->getAllByCach();

        dump($data);
    }
}
